Python 3.11.0 | packaged by conda-forge | (main, Jan 16 2023, 14:12:30) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.2 -- An enhanced Interactive Python. Type '?' for help.

In [ ]:
import os
import sys
from functools import partial
sys.path.append(os.path.join(os.getcwd(), '..')) #adds directory below as valid path
from  datetime import datetime, timedelta
dateformat = "%H-%M-%S"
from collections import deque
import traceback
from multiprocessing import Pool
from tqdm import tqdm

import scipy.constants as spc
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rcParams['axes.grid'] = True
plt.rcParams['grid.linestyle'] = '--'

from MT_class_PID_new import MTdataHost
from global_folder.myplotsty import *
from global_folder.my_helpers import *

PUMP_FREQUENCY = 384228.6
REPUMP_FREQUENCY = 384228.6 + 6.56
SAMPLE_RATE = 2000
FREQVSVOLT = 221.0 
FREQVSCURR = 1.13

# TODO: find a better place for this
EXP_FOLDER =r'C:\Users\svars\OneDrive\Desktop\UBC Lab\CATExperiment\CATMeasurements'
MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPArun16')
WDATA_FOLDER =os.path.join(MEASURE_FOLDER, 'testPArun16.csv')

# TODO: maybe make a run analysis class out of this?
def dump():
	fig, ax = plt.subplots()
	plot_results(ax, df, max_freq= 384219., fmt='o', mfc='red', save_folder=MEASURE_FOLDER)
	plt.savefig(os.path.join(MEASURE_FOLDER, 'ratio_vs_freq.png'))

	
	collect_plots(MEASURE_FOLDER, os.path.join(MEASURE_FOLDER, 'collected_plots'), 'deloadPhase.png')
 
	#*-----------------------
	#* SINGLE RUN
 	#*----------------------- 
  
	MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPArun9')
	df = get_data_frame(MEASURE_FOLDER)
	df.dropna(inplace=True)
	#freqs = plot_results(df, 384201., save_folder=MEASURE_FOLDER)
 
	data = df.dropna()
	freqs = ((384201-PUMP_FREQUENCY)-(data['tempV']-df['tempV'].min())*FREQVSVOLT- (data['currV']-df['currV'].min())*FREQVSCURR)
	fig, ax = plt.subplots()
	plot_spline_fit(ax=ax, x=freqs, y=data['ratio'], yerr=data['ratioErr'], 
				 s=0.1, save_folder=MEASURE_FOLDER, 
				 mfc='red', color='black', 
				 title='Trap Depth = 1.99 K')
 
	# *-----------------------
	# * MULTIPLE RUN COMPARISON
 	# *-----------------------
	folders = [os.path.join(EXP_FOLDER, path ) for path in ['testPARun11', 'testPARun12', 'testPARun13', 'testPARun14']]
	dfs = [get_data_frame(measure_folder, cache_all=True) for measure_folder in folders]

	colors = ['red', 'dodgerblue', 'green', 'grey']
	max_freqs = [384178.881, 384178.593,384179.068, 384184.731]
	#max_freqs=[384178.599, 384179.091, 384184.733]

	zipped_data = list(zip(dfs, max_freqs))
	fig, ax = plt.subplots()
	for i, (df, max_freq)  in enumerate(zipped_data[:3]):
		data = df.dropna()
		freqs = ((max_freq-PUMP_FREQUENCY)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
		color = colors[i]
		ax=plot_spline_fit(ax, x=freqs, y=data['ratio'], scolor=color, mfc=color,color=color, s=0.02, ms=5)

	#*-----------------------
	#* PARSING WAVEMETER DATA
 	#*-----------------------

	MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPArun14')
	WDATA_FOLDER =os.path.join(MEASURE_FOLDER, 'testPArun14.csv')
	freq_data, max_freq, min_freq = add_wavemeter_data('', WDATA_FOLDER)
	data = freq_data[:]
	levels = staircase_fit(data)

	data = get_data_frame(MEASURE_FOLDER)
	data.dropna(inplace=True)
	freqs = ((max_freq)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
	plt.plot(freqs)
 
	#*-----------------------
	#* GETTING DEPTH_RATIO DATAFRAME
 	#*-----------------------
	MEASURE_FOLDER = r'C:\Users\svars\OneDrive\Desktop\UBC Lab\CATExperiment\CATMeasurements\relScatRate'
	depth_ratios_df = get_rel_scattering_df(MEASURE_FOLDER)
	df_slice = depth_ratios_df[(depth_ratios_df['pa1']==1.85) & (depth_ratios_df['pd1']==84) & (depth_ratios_df['pd2'] == 84)]
	x = df_slice['pa2']
	y = df_slice['depth_ratio']
	plt.plot(x,y, 'o')

	#*-----------------------
	#* MEGA_RUN
 	#*-----------------------	
	MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPARunMega')
	df = get_data_frame(MEASURE_FOLDER)
	df.dropna(inplace=True)

	df_grouped = df.groupby(by='pump_reference')
	min_ratios = df_grouped['ratio'].min()

	groups = dict(list(df_grouped))
	dfs = [df for df in groups.values()]
 
	max_freqs = [384182.5]*15
	#max_freqs=[384178.599, 384179.091, 384184.733]
	zipped_data = list(zip(dfs, max_freqs))
	fig, ax = plt.subplots()
	for i, (df, max_freq)  in enumerate(zipped_data[:]):
		data = df.dropna()
		freqs = ((max_freq-PUMP_FREQUENCY)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
		ax=plot_spline_fit(ax, x=freqs, y=data['ratio'], yerr=data['ratioErr'],scolor=f'C{i}', mfc=f'C{i}',color=f'C{i}', s=0.0, ms=5, figsize=(10, 10), label=f"Pump Amplituide = { df.iloc[10]['pump_reference'] :.2f}", linewidth=2.5)

	plt.legend()

	plt.savefig(os.path.join(MEASURE_FOLDER, 'lossFeatures.png'))
 
	x = [(180-2*df['pump_AOM_freq'].mean()) for df in dfs]
	y = [(df['ratio'].max() - df['ratio'].min()) for df in dfs]
	plt.plot( x, y ,'-o', label=fr"$\delta$ = {180 - 2*dfs[0]['pump_AOM_freq'].mean()} MHz")
	plt.xlabel(r'$\delta$ (MHz)')
	plt.ylabel(r'SNR $ = V_{ss, off} - V_{ss, on}$ ')
 
	x = [df['pump_reference'].mean() for df in dfs]
	y = [(df['ratio'].max() - df['ratio'].min()) for df in dfs]
	plt.plot( x, y ,'-o', label=f"Pump Amplitude = {df['pump_reference'].mean()}")
	plt.xlabel('Pump Amplitude')
	plt.ylabel(r'SNR $ = V_{ss, off} - V_{ss, on}$ ')
	plt.title(fr"$\delta $ ={180-2*df['pump_AOM_freq'].mean()}")
	#*-----------------------
	#* MULTIPLE MEGARUN
	#*-----------------------
 
	folders = [os.path.join(EXP_FOLDER, path ) for path in ['testPArunMega', 'testPArunMega2']]
	dfs_mega = [get_data_frame(measure_folder, cache_all=True).dropna() for measure_folder in folders]

	dfs_grouped = [df_mega.groupby(by='pump_reference') for df_mega in dfs_mega]
	min_ratios = [df_grouped['ratio'].min() for df_grouped in dfs_grouped]

	groupss = [dict(list(df_grouped)) for df_grouped in dfs_grouped]
	dfs = [ [df for df in groups.values()] for groups in groupss]
 
	for row in dfs:
		x = [df['pump_reference'].mean() for df in row]
		y = [(df['ratio'].max() - df['ratio'].min())/df['motSS'].std() for df in row]
		plt.plot( x, y ,'-o', label=fr"$\delta$ = {180 - 2*row[0]['pump_AOM_freq'].mean()} MHz")
		plt.xlabel('Pump Amplitude')
		plt.ylabel(r'SNR $ = \frac{V_{ss, off} - V_{ss, on}}{\sigma_{V,off}}$ ')
	plt.legend()

def freq_misc():
	WDATA_FOLDER = r'C:\Users\svars\OneDrive\Desktop\UBC Lab\CATExperiment\CATMeasurements\CATcurrTestrun3.csv'
	freq_data = add_wavemeter_data('', WDATA_FOLDER)
	levels = staircase_fit(freq_data[0], peak_height=0.2, distance=50, data_offset=1, window_size=1) 

	plt.close()
 
	x = np.linspace(0, 4.9, 25)
	y = levels
 
	plt.plot(levels, '-o')
	plt.title('Levels plot')
	
	m, b, fit_line = my_linear_fit(x, y)
def save_fit_results(run_path, plot=False):
	filename = os.path.join(run_path, 'data.csv')
	bkfilename = os.path.join(run_path, 'data_oldPD.csv')
	settingsname = os.path.join(run_path, 'Settings.txt')
	
	dataHost = MTdataHost(SAMPLE_RATE)
	dataHost.loadCATdata(fileName=filename, settingsName=settingsname)
	dataHost.CATbackgroundData(bkfilename)
	
	dataHost.setAllCAT(0.002)
	resultDict = dataHost.getResults(run_path, store=True)
 
	if plot:
		dataHost.storeFits(run_path, combined=True, separate=True)
  
	return resultDict, dataHost.settings
		
def get_timestamp(run_path):
	timestamp = datetime.strptime(os.path.split(run_path)[-1].split('_')[0], dateformat)
	return timestamp

def extract_fit(run_path, plot=True, cache_failed=True, cache_all=True):
	"""Gather relevant data from each measurement run

	Args:
		run_path : absolute path to the run directory
		plot (bool, optional): plot fits. Defaults to True.
		cache_failed (bool, optional): Cache failed fits. If false, refit. Doesn't refit non-failed fits. Defaults to True.
		cache_all (bool, optional): If false, ignore any cached fit_results. Defaults to True.

	Returns:
		a 3-tuple (fit_results, settings, timestamp)
	"""
	fit_results, settings, timestamp = {}, {}, None
	
	if not os.path.isdir(run_path):
		return fit_results, settings, timestamp  # directory is not a run directory

	try:
		timestamp = get_timestamp(run_path)
  
  	# TODO: specify which error to catch
	except Exception as e:
		print("Error extracting timestamp from: ", run_path)
		print(traceback.format_exc())
	
	MAT_fit_cache_path = os.path.join(run_path, 'resultDict.txt')
 
	if not os.path.exists(MAT_fit_cache_path) or not cache_all:
		
		try: 
			fit_results, settings = save_fit_results(run_path, plot=plot)
		except Exception as e:
			print(traceback.format_exc())
			print("Fitting ERROR at ", os.path.basename(run_path), '\n')

			with open(MAT_fit_cache_path, 'w') as f:
				f.write(str('MAT fit failed'))
		
	else:
		print("Accessing cached results from :", os.path.basename(run_path))
  
		fit_results = open(MAT_fit_cache_path, 'r').read()

		if fit_results == 'MAT fit failed':
			if not cache_failed:
				# fit regardless of cached result
				try: 
					fit_results, settings = save_fit_results(run_path, plot=plot)
				except Exception as e:
					print(traceback.format_exc())
					print("Fitting ERROR at ", os.path.basename(run_path), '\n')
	
					with open(MAT_fit_cache_path, 'w') as f:
						f.write(str('MAT fit failed'))	
			else:
				print("Failed fit at :", os.path.basename(run_path))
				fit_results = {}

		else:
			fit_results = eval(open(MAT_fit_cache_path, 'r').read())
   
			settingsname = os.path.join(run_path, 'Settings.txt')
			settings = eval(open(settingsname, 'r').read())
   
	return fit_results, settings, timestamp



def get_row(run_path, **kwargs):
			
   fit_results, settings, timestamp = extract_fit(run_path, **kwargs)
   row = {**fit_results, **settings, **{'timestamp':timestamp}}
   
   return row

   
def get_data_frame(data_dir, parallel=True, in_process_run=False, **kwargs):
   
	run_path_arr = []
	rows = []
	
	for relative_path in os.listdir(data_dir):
		run_path_arr.append(os.path.join(data_dir, relative_path))
  
	if in_process_run:
		run_path_arr.pop()

	run_path_arr = sorted(run_path_arr)
	
	if parallel:
		with Pool(4) as p:
			rows = list(tqdm(p.imap(partial(get_row, **kwargs), run_path_arr), total=len(run_path_arr)))
	
	else:
		for run_path in tqdm(run_path_arr):
		   rows.append(get_row(run_path, **kwargs))

	return pd.DataFrame.from_dict(rows)

def add_wavemeter_data(df, wmeter_csv_path, window_size=100, num_rows=50):


	"""Extract unique frequnecy values from wavemeter data
	Returns:
		unique_levels (list): unique frequency values in wavemeter data
	"""
 
	# TODO: modify dataframe in place with frequency data
 
	wdata = pd.read_csv(wmeter_csv_path, skiprows=2)
	wdata.dropna(inplace=True)
	freq_data = np.array(wdata.iloc[:, 0])
	try:
		freq_data = np.array([float(item) for item in freq_data if item.replace('.','').isdigit()])
	except Exception as e:
		print(e)
 
	max_freq = freq_data.max()
	min_freq = freq_data.min()

	return freq_data, max_freq, min_freq

def plot_results(ax, dfs, max_freq, min_freq=0.0, mfc='red', fmt='o', ms=5, save_folder=False, xscale=1.0, yscale=1.0, **kwargs):
	FREQVSVOLT = 221.0 
	FREQVSCURR = 1.13
	
	if not type(dfs) == list:
		freqs = ((max_freq-PUMP_FREQUENCY)-(dfs.dropna()['tempV']-dfs.dropna()['tempV'].min())*FREQVSVOLT- (dfs.dropna()['currV']-dfs.dropna()['currV'].min())*FREQVSCURR)*xscale
		dfs=[dfs]
  
	plt.gcf().set_dpi(300)
	
	for df in dfs:
		df = df.dropna()
		ax.errorbar(freqs, 
			   df['ratio']*yscale,
			   yerr=df['ratioErr'],
			   fmt=fmt, mfc=mfc, color='black', ms=ms, **kwargs)
		ax.set_ylabel(r'$\mathbf{\frac{V_{ss, cat}}{V_{ss}}} $ ', **labeldict)
		ax.set_xlabel(r'$\Delta $ (GHz)', **labeldict)
	
	if save_folder:
		plt.savefig(os.path.join(save_folder, 'ratio_vs_freq.png'))
	
	return freqs, ax
  
	#return plt.gca(), plt.gcf()
	#plt.show()

def plot_spline_fit(ax, x, y, s=1, yerr=None, color='black', scolor='black',figsize=(12,5), save_folder=None, title='',alpha=0.5,dpi=200, label='plot',**kwargs):
	from scipy.interpolate import splev, splrep
	xnew = np.linspace(min(x), max(x), 3*len(x) )

	y = [b for a,b in sorted(zip(x,y), key=lambda pair: pair[0])]
	if yerr is not None:
		yerr = [b for a,b in sorted(zip(x,yerr), key=lambda pair: pair[0])]
 
	x = sorted(x)

	spl = splrep(x, y, s=s)
	ynew = splev(xnew, spl)

	
	plt.gcf().set_dpi(dpi)
	plt.gcf().set_size_inches(figsize)
	
	if yerr is not None:
		ax.errorbar(x, y, yerr=yerr, fmt='o', **kwargs)
	else:
		ax.plot(x,y, 'o', **kwargs)
	ax.plot(xnew, ynew, '-', color=scolor, alpha=alpha, label=label, **kwargs)
 
	ax.set_ylabel(r'$\mathbf{\frac{V_{ss, cat}}{V_{ss}}} $ ', **labeldict)
	ax.set_xlabel(r'$\Delta $ (GHz)', **labeldict)
 
	ax.set_title(title, **titledict)
	if save_folder:
		plt.savefig(os.path.join(save_folder, 'spline_ratio_vs_freq.png'))
	
	return ax
 
def plot_polyfit(x_data, y_data, spline_degree):
	
	coefficients = np.polyfit(x_data, y_data, spline_degree)

	x_interp = np.linspace(min(x_data), max(x_data), 100)
	y_interp = np.polyval(coefficients, x_interp)

	plt.scatter(x_data, y_data, label='Original Data')
	plt.plot(x_interp, y_interp, label='Polynomial Interpolation (Degree={})'.format(spline_degree))

def collect_plots(source, destination, plot_name):
	print(f'Collecting plots from {os.path.basename(source)}')
	import shutil

	os.makedirs(destination, exist_ok=True)

	plot_files = []
	for root, dirs, files in os.walk(source):
		
		for file in files:
			if file == plot_name:
				plot_files.append(os.path.join(root, file))

	for i, plot_file in enumerate(plot_files, start=0):
		new_filename = f'{i}{plot_name}'
		destination_path = os.path.join(destination, new_filename)
		shutil.copy(plot_file, destination_path)

def create_GIF(images_folder, image_name):
	import imageio
	with imageio.get_writer(os.path.join(images_folder, f'{image_name}movie.gif'), mode='I', duration=0.5) as writer:
		for filename in os.listdir(images_folder):
			if image_name in filename:
				image = imageio.imread(os.path.join(images_folder, filename))
				writer.append_data(image)

def staircase_fit(data, peak_height=0.1, distance=100, data_offset=1, window_size=1, inc_final_peak=True):
	def moving_average(arr, window_size):
		weights = np.ones(window_size) / window_size
		return np.convolve(arr, weights, mode='valid')

	convdata1 = moving_average(data, window_size)
	convdata2 = moving_average(data[data_offset+1:], window_size )
	final = convdata1[:len(convdata2)]-convdata2
	# plt.plot(data)
	# plt.plot(convdata1)
	# plt.plot(convdata2)
	# plt.show()
	# plt.plot(final)

	
	from scipy.signal import find_peaks
	x= final
	peaks, _ = find_peaks(x, height=peak_height, distance=distance)
	peaks = np.insert(peaks, 0, 0)
	levels = []
	plot_arr = []
	for i, peak in enumerate(peaks):
		
		if i < len(peaks) - 1:
			temp = data[ peaks[i]:peaks[i+1] ]
			plot_arr.extend( np.ones_like(temp)*np.mean(temp))
			levels.append(np.mean(temp))
	if inc_final_peak:
		temp = data[peaks[-1]:]
		levels.append(np.mean(temp))
		plot_arr.extend( np.ones_like(temp)*np.mean(temp))
  
	plt.plot(x)
	plt.plot(peaks, x[peaks], "x")
	plt.plot(np.zeros_like(x), "--", color="gray")
	plt.show()
	
	plt.plot(data)
	plt.plot(np.ravel((plot_arr)))
	plt.show()
	plt.close()
	plt.plot(np.array(levels)[np.where(abs(np.diff(levels))>0.05)[0]], 'o', ms=5)
 
	return levels

def load_single_run(run_path):
	filename = os.path.join(run_path, 'data.csv')
	bkfilename = os.path.join(run_path, 'data_oldPD.csv')
	settingsname = os.path.join(run_path, 'Settings.txt')
	
	dh1 = MTdataHost(SAMPLE_RATE)
	dh1.loadCATdata(fileName=filename, settingsName=settingsname)
	
	return dh1
	



if __name__ == '__main__':
	# run_path = r"C:\Users\svars\OneDrive\Desktop\UBC Lab\CATExperiment\CATMeasurements\testPArun9\16-53-10"
	# filename = os.path.join(run_path, 'data.csv')
	# bkfilename = os.path.join(run_path, 'data_oldPD.csv')
	# settingsname = os.path.join(run_path, 'Settings.txt')
	
	# dh1 = MTdataHost(SAMPLE_RATE)
	# dh1.loadCATdata(fileName=filename, settingsName=settingsname)
	
	# dh1.setAllCAT(0.002)
 
 	#dh1.CATbackgroundData(bkfilename)
	
	pass
In [ ]:
MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPArunFull2')
df = get_data_frame(MEASURE_FOLDER)
df.dropna(inplace=True)
100%|██████████| 790/790 [00:05<00:00, 140.17it/s]
In [ ]:
dfc = df.copy()
df_grouped = df.groupby(by=['pump_reference', 'pump_AOM_freq'])
In [ ]:
groups = dict(list(df_grouped))
In [ ]:
len(groups)
Out[ ]:
20
In [ ]:
dfs = [df for df in groups.values()]
In [ ]:
SNRs = [(df['ratio'].max() - df['ratio'].min()) for df in dfs]
In [ ]:
SNRs = np.array(SNRs).reshape(4,5)
In [ ]:
SNRs
Out[ ]:
array([[0.13378895, 0.28871934, 0.34506887, 0.31684751, 0.28913976],
       [0.3944464 , 0.36689669, 0.30371337, 0.29546041, 0.31586858],
       [0.34860603, 0.29497902, 0.29564968, 0.88648202, 0.33900947],
       [0.31031989, 0.46329302, 0.29125016, 0.25143005, 0.21669097]])
In [ ]:
pd_pa_arr = [(df['pump_AOM_freq'].mean(), df['pump_reference'].mean()) for df in dfs]
In [ ]:
np.array(pd_pa_arr)
Out[ ]:
array([[83.    ,  0.4   ],
       [84.    ,  0.4   ],
       [85.    ,  0.4   ],
       [86.    ,  0.4   ],
       [83.    ,  0.7625],
       [84.    ,  0.7625],
       [85.    ,  0.7625],
       [86.    ,  0.7625],
       [83.    ,  1.125 ],
       [84.    ,  1.125 ],
       [85.    ,  1.125 ],
       [86.    ,  1.125 ],
       [83.    ,  1.4875],
       [84.    ,  1.4875],
       [85.    ,  1.4875],
       [86.    ,  1.4875],
       [83.    ,  1.85  ],
       [84.    ,  1.85  ],
       [85.    ,  1.85  ],
       [86.    ,  1.85  ]])
In [ ]:
np.array(pd_pa_arr).reshape(5,4, 2)
Out[ ]:
array([[[83.    ,  0.4   ],
        [84.    ,  0.4   ],
        [85.    ,  0.4   ],
        [86.    ,  0.4   ]],

       [[83.    ,  0.7625],
        [84.    ,  0.7625],
        [85.    ,  0.7625],
        [86.    ,  0.7625]],

       [[83.    ,  1.125 ],
        [84.    ,  1.125 ],
        [85.    ,  1.125 ],
        [86.    ,  1.125 ]],

       [[83.    ,  1.4875],
        [84.    ,  1.4875],
        [85.    ,  1.4875],
        [86.    ,  1.4875]],

       [[83.    ,  1.85  ],
        [84.    ,  1.85  ],
        [85.    ,  1.85  ],
        [86.    ,  1.85  ]]])
In [ ]:
SNRs = SNRs.reshape(5,4)
In [ ]:
plt.imshow(SNRs)
plt.xlabel('Pump Reference')
plt.ylabel('Pump Detuniing')
Out[ ]:
Text(0, 0.5, 'Pump Detuniing')
In [ ]:
df_grouped['ratio'].max() - df_grouped['ratio'].min()
Out[ ]:
pump_reference  pump_AOM_freq
0.4000          83.0             0.133789
                84.0             0.288719
                85.0             0.345069
                86.0             0.316848
0.7625          83.0             0.289140
                84.0             0.394446
                85.0             0.366897
                86.0             0.303713
1.1250          83.0             0.295460
                84.0             0.315869
                85.0             0.348606
                86.0             0.294979
1.4875          83.0             0.295650
                84.0             0.886482
                85.0             0.339009
                86.0             0.310320
1.8500          83.0             0.463293
                84.0             0.291250
                85.0             0.251430
                86.0             0.216691
Name: ratio, dtype: float64
In [ ]:
max_freqs = [384182.5]*30
#max_freqs=[384178.599, 384179.091, 384184.733]
zipped_data = list(zip(dfs, max_freqs))
fig, ax = plt.subplots()
for i, (df, max_freq)  in enumerate(zipped_data[:]):
    data = df.dropna()
    freqs = ((max_freq-PUMP_FREQUENCY)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
    
    ax=plot_spline_fit(ax, x=freqs, y=data['ratio'], yerr=data['ratioErr'],scolor=f'C{i}', mfc=f'C{i}',color=f'C{i}', s=0.0, ms=5, figsize=(10, 10), linewidth=2.5)
    
    plt.title(f"Pump Amplituide = { df.iloc[10]['pump_reference'] :.2f}, \
                sDetuning  = { 180-2*df.iloc[10]['pump_AOM_freq'] :.2f}", **titledict)

    plt.legend()

    plt.savefig(os.path.join(MEASURE_FOLDER, f'lossFeatures{i}.png'))
    plt.show()
    
    plt.close()
    fig, ax = plt.subplots()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[36], line 11
      7 freqs = ((max_freq-PUMP_FREQUENCY)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
      9 ax=plot_spline_fit(ax, x=freqs, y=data['ratio'], yerr=data['ratioErr'],scolor=f'C{i}', mfc=f'C{i}',color=f'C{i}', s=0.0, ms=5, figsize=(10, 10), linewidth=2.5)
---> 11 plt.title(f"Pump Amplituide = { df.iloc[10]['pump_reference'] :.2f}, \
     12             sDetuning  = { 180-2*df.iloc[10]['pump_AOM_freq'] :.2f}", **titledict)
     14 plt.legend()
     16 plt.savefig(os.path.join(MEASURE_FOLDER, f'lossFeatures{i}.png'))

File c:\ProgramData\Anaconda3\envs\magpy_env\Lib\site-packages\pandas\core\indexing.py:1073, in _LocationIndexer.__getitem__(self, key)
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1069'>1070</a> axis = self.axis or 0
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1071'>1072</a> maybe_callable = com.apply_if_callable(key, self.obj)
-> <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1072'>1073</a> return self._getitem_axis(maybe_callable, axis=axis)

File c:\ProgramData\Anaconda3\envs\magpy_env\Lib\site-packages\pandas\core\indexing.py:1625, in _iLocIndexer._getitem_axis(self, key, axis)
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1621'>1622</a>     raise TypeError("Cannot index by location index with a non-integer key")
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1623'>1624</a> # validate the location
-> <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1624'>1625</a> self._validate_integer(key, axis)
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1626'>1627</a> return self.obj._ixs(key, axis=axis)

File c:\ProgramData\Anaconda3\envs\magpy_env\Lib\site-packages\pandas\core\indexing.py:1557, in _iLocIndexer._validate_integer(self, key, axis)
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1554'>1555</a> len_axis = len(self.obj._get_axis(axis))
   <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1555'>1556</a> if key >= len_axis or key < -len_axis:
-> <a href='file:///c%3A/ProgramData/Anaconda3/envs/magpy_env/Lib/site-packages/pandas/core/indexing.py?line=1556'>1557</a>     raise IndexError("single positional indexer is out-of-bounds")

IndexError: single positional indexer is out-of-bounds
In [ ]:
SNRdata = df_grouped['ratio'].max() - df_grouped['ratio'].min()
In [ ]:
SNRdata.index
Out[ ]:
MultiIndex([(               0.4, 83.0),
            (               0.4, 84.0),
            (               0.4, 85.0),
            (               0.4, 86.0),
            (0.7625000000000001, 83.0),
            (0.7625000000000001, 84.0),
            (0.7625000000000001, 85.0),
            (0.7625000000000001, 86.0),
            (             1.125, 83.0),
            (             1.125, 84.0),
            (             1.125, 85.0),
            (             1.125, 86.0),
            (1.4875000000000003, 83.0),
            (1.4875000000000003, 84.0),
            (1.4875000000000003, 85.0),
            (1.4875000000000003, 86.0),
            (              1.85, 83.0),
            (              1.85, 84.0),
            (              1.85, 85.0),
            (              1.85, 86.0)],
           names=['pump_reference', 'pump_AOM_freq'])
In [ ]:
SNRdf = SNRdata.reset_index()
SNRdf.columns = ['pump_reference', 'pump_AOM_freq', 'SNR']
In [ ]:
SNRdf
Out[ ]:
pump_reference pump_AOM_freq SNR
0 0.4000 83.0 0.133789
1 0.4000 84.0 0.288719
2 0.4000 85.0 0.345069
3 0.4000 86.0 0.316848
4 0.7625 83.0 0.289140
5 0.7625 84.0 0.394446
6 0.7625 85.0 0.366897
7 0.7625 86.0 0.303713
8 1.1250 83.0 0.295460
9 1.1250 84.0 0.315869
10 1.1250 85.0 0.348606
11 1.1250 86.0 0.294979
12 1.4875 83.0 0.295650
13 1.4875 84.0 0.886482
14 1.4875 85.0 0.339009
15 1.4875 86.0 0.310320
16 1.8500 83.0 0.463293
17 1.8500 84.0 0.291250
18 1.8500 85.0 0.251430
19 1.8500 86.0 0.216691
In [ ]:
pivot_table = SNRdf.pivot('pump_reference', 'pump_AOM_freq', 'SNR')
xticklabels = [f'{x:.2f}' for x in pivot_table.columns]
yticklabels = [f'{y:.2f}' for y in pivot_table.index]
sns.heatmap(pivot_table, annot=True, fmt='.2f', xticklabels=xticklabels, yticklabels=yticklabels)
plt.grid()
<ipython-input-48-d40ecd36e3f8>:1: FutureWarning: In a future version of pandas all arguments of DataFrame.pivot will be keyword-only.
  pivot_table = SNRdf.pivot('pump_reference', 'pump_AOM_freq', 'SNR')
In [ ]:
MEASURE_FOLDER = os.path.join(EXP_FOLDER, 'testPArunFull3')
df = get_data_frame(MEASURE_FOLDER)
df.dropna(inplace=True)
100%|██████████| 402/402 [28:05<00:00,  4.19s/it]
In [ ]:
dfc = df.copy()
df_grouped = df.groupby(by=['pump_reference', 'pump_AOM_freq'])

SNR_temp = df_grouped['ratio'].max() - df_grouped['ratio'].min()
SNRdf = SNR_temp.reset_index()
In [ ]:
SNRdf.columns = ['pump_reference', 'pump_AOM_freq', 'SNR']
pivot_table = SNRdf.pivot('pump_reference', 'pump_AOM_freq', 'SNR')
xticklabels = [f'{x:.2f}' for x in pivot_table.columns]
yticklabels = [f'{y:.2f}' for y in pivot_table.index]
sns.heatmap(pivot_table, annot=True, fmt='.2f', xticklabels=xticklabels, yticklabels=yticklabels)
plt.grid()
<ipython-input-51-c448096c4f8e>:2: FutureWarning: In a future version of pandas all arguments of DataFrame.pivot will be keyword-only.
  pivot_table = SNRdf.pivot('pump_reference', 'pump_AOM_freq', 'SNR')
In [ ]:
groups = dict(list(df_grouped))
dfs = [df for df in groups.values()]

max_freqs = [384182.5]*30
#max_freqs=[384178.599, 384179.091, 384184.733]
zipped_data = list(zip(dfs, max_freqs))
fig, ax = plt.subplots()
for i, (df, max_freq)  in enumerate(zipped_data[:]):
    data = df.dropna()
    freqs = ((max_freq-PUMP_FREQUENCY)-(data['tempV']-data['tempV'].min())*FREQVSVOLT- (data['currV']-data['currV'].min())*FREQVSCURR)
    
    ax=plot_spline_fit(ax, x=freqs, y=data['ratio'], yerr=data['ratioErr'],scolor=f'C{i}', mfc=f'C{i}',color=f'C{i}', s=0.0, ms=5, figsize=(10, 10), linewidth=2.5)
    
    plt.title(f"Pump Amplituide = { df.iloc[10]['pump_reference'] :.2f}, \
                sDetuning  = { 180-2*df.iloc[10]['pump_AOM_freq'] :.2f}", **titledict)

    plt.legend()

    plt.savefig(os.path.join(MEASURE_FOLDER, f'lossFeatures{i}.png'))
    plt.show()
    
    plt.close()
    fig, ax = plt.subplots()